package cc.linker.steplink.crm.service.impl;

import cc.linker.steplink.crm.dao.ScoreActDao;
import cc.linker.steplink.crm.domain.ScoreAct;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.service.IScoreActService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class ScoreActServiceImpl implements IScoreActService {

    @Autowired
    private ScoreActDao ScoreActDao;

    @Override
    public List<Map<String, Object>> listScoreAct(Map map) {
        return ScoreActDao.listScoreAct(map);
    }

    @Override
    public Boolean addScoreAct(Map record) throws Exception {
        if(1 == ScoreActDao.addScoreAct(record)){
            return true;
        }
        return false;
    }

    @Override
    public Boolean delScoreAct(String scoreActId) throws Exception {
        if(1 == ScoreActDao.deleteScoreAct(scoreActId)){
            return true;
        }
        return false;
    }

    @Override
    public void editScoreAct(Map record) {
        ScoreActDao.editScoreAct(record);
    }

    @Override
    public HashMap<String, Object> getScoreActInfo(String scoreActId) {
        return ScoreActDao.getScoreActInfo(scoreActId);
    }

    @Override
    public Map<String, Object> getInfo(String scoreactId){
        return ScoreActDao.getInfo(scoreactId);
    }

    @Override
    public Map<String, Object> getGoods(String goodsId){
        return ScoreActDao.getGoods(goodsId);
    }

    @Override
    public void changeStatus(Map<String, Object> map){
        if("1".equals(map.get("status").toString())){
            String id = map.get("scoreactId").toString();
            Map<String, Object> info = ScoreActDao.getInfo(id);
            if(null == info.get("rule")){
                throw GlobalException.build("请先配置任务规则");
            }
            String rule = info.get("rule").toString();
            if("custom".equals(info.get("activeType").toString())){
                List<Map<String,Object>> rul = (List<Map<String,Object>>) JSON.parse(rule);
                for(Map<String,Object> r:rul){
                    if(StringUtils.isEmpty(r.get("sendVal"))){
                        throw GlobalException.build("请先配置任务规则");
                    }
                }
            }else{
                Map<String,Object> ru = (Map) JSON.parse(rule);
                if(StringUtils.isEmpty(ru.get("val1")) || StringUtils.isEmpty(ru.get("val2"))){
                    throw GlobalException.build("请先配置任务规则");
                }
            }
        }
        ScoreActDao.changeStatus(map);
    }

    @Override
    public Map<String, Object> getGoodsIds(Map<String, Object> param){
        Map<String,Object> data = new HashMap<>();
        List<Map<String, Object>> list = ScoreActDao.getCustomList(param);
        List<Integer> goodsIds = new ArrayList<>();
        if(null!=list && list.size()>0){
            for(Map<String, Object> li:list){
                if(!StringUtils.isEmpty(li.get("rule"))){
                    List<Map<String, Object>> rule = (List<Map<String, Object>>)JSON.parse(li.get("rule").toString());
                    if(rule.size()>0){
                        for(Map<String, Object> ru:rule){
                            if(!StringUtils.isEmpty(ru.get("goodsId"))){
                                goodsIds.add(Integer.valueOf(ru.get("goodsId").toString()));
                            }
                        }
                    }
                }
            }
        }
        List lista = goodsIds.stream().distinct().collect(Collectors.toList());
        data.put("goodsIds",lista);
        return data;
    }

    @Override
    public int countScoreAct(Map<String, Object> map){
        return ScoreActDao.countScoreAct(map);
    }

    @Override
    public int countScoreActBase(Map<String, Object> map){
        return ScoreActDao.countScoreActBase(map);
    }
}
